const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
// 判断输入字符串是否为空或者全部都是空格
function isNull(str) {
  if (str == "") return true;
  if (str == undefined) return true;
  if (str == null) return true;
  var regu = "^[ ]+$";
  var re = new RegExp(regu);
  return re.test(str);
}
//去除空格
function removeSpace(str) {
  return str.replace(/\s+/g, "");
}
//相加精度
function accAdd(num1, num2) {
  var r1, r2, m;
  try {
    r1 = num1.toString().split('.')[1].length;
  } catch (e) {
    r1 = 0;
  }
  try {
    r2 = num2.toString().split(".")[1].length;
  } catch (e) {
    r2 = 0;
  }
  m = Math.pow(10, Math.max(r1, r2));
  // return (num1*m+num2*m)/m;  
  return Math.round(num1 * m + num2 * m) / m;
}

// 相减精度
function accSub(num1, num2) {
  var r1, r2, m, n;
  try {
    r1 = num1.toString().split('.')[1].length;
  } catch (e) {
    r1 = 0;
  }
  try {
    r2 = num2.toString().split(".")[1].length;
  } catch (e) {
    r2 = 0;
  }
  m = Math.pow(10, Math.max(r1, r2));
  n = (r1 >= r2) ? r1 : r2;
  return (Math.round(num1 * m - num2 * m) / m).toFixed(n);
}

//相乘精度
function formatFloat(arg1, arg2) {
  var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
  try { m += s1.split(".")[1].length } catch (e) { }
  try { m += s2.split(".")[1].length } catch (e) { }
  return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
}

//请求访问接口
function request(object) {
  object.method = object.method ? object.method : 'GET'
  if (object.method == 'GET') {
    wx.request({
      url: object.url,
      data: object.data,
      method: 'GET',
      header: {
        deviceType: '03',
        env: app.globalData.environment,
      },
      success: function (res) {
        if (res.data.error_code == 0) {
          object.success(res);
        } else if (res.data.error_code == 403) {
          app.globalData.showLoginDialog = true;
          wx.showModal({
            title: '提示',
            content: '该账户已在别处登录，是否跳转至登录页面？',
            success(res) {
              if (res.confirm) {
                wx.redirectTo({
                  url: '../login/login',
                })
              } else { }
            }
          })
        } else {
          console.log(res)
          wx.showToast({
            title: res.data.reason,
            icon: 'loading',
            duration: 2000
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: res,
          icon: 'loading',
          duration: 2000
        })
      },
      complete: function (res) { }
    })
  } else if (object.method == 'POST') {
    wx.request({
      url: object.url,
      data: object.data,
      method: 'POST',
      header: {
        deviceType: '03',
        env: app.globalData.environment,
        "Content-Type": "application/x-www-form-urlencoded",
      },
      success: function (res) {

        if (res.data.error_code == 0) {
          object.success(res);
        } else if (res.data.error_code == 403) {
          app.globalData.showLoginDialog = true;
          wx.showModal({
            title: '提示',
            content: '该账户已在别处登录，是否跳转至登录页面？',
            success(res) {
              if (res.confirm) {
                wx.redirectTo({
                  url: '../login/login',
                })
              } else { }
            }
          })
        } else {
          console.log(res)
          try {
            wx.showToast({
              title: res.data.reason,
              icon: 'loading',
              duration: 2000
            })
          } catch (e) { }
        }
      },
      fail: function (res) {
        console.log(res)
        try {
          wx.showToast({
            title: res,
            icon: 'loading',
            duration: 2000
          })
        } catch (e) { }
      },
      complete: function (res) { }
    })
  }
}
module.exports = {
  formatTime: formatTime,
  removeSpace: removeSpace,
  isNull: isNull,
  accAdd: accAdd,
  accSub: accSub,
  formatFloat: formatFloat,
  request: request,
}
